/*
 * @lc app=leetcode.cn id=859 lang=javascript
 *
 * [859] 亲密字符串
 */

// @lc code=start
/**
 * @param {string} a
 * @param {string} b
 * @return {boolean}
 */
// var buddyStrings = function(a, b) {
//   if(a.length !== b.length) return false;
//   if(a === b) return a.length > new Set(a).size;

//   let diff = [];
//   let i = 0;
//   while(a[i]){
//     if(a[i] !== b[i]) diff.push(a[i]),diff.push(b[i]);
//     i++;
//   }
//   return diff.length === 4 && diff[0] === diff[3] && diff[1] === diff[2];
  
// };

/**
 * 复习 2021-08-04
 */
var buddyStrings = function(a, b) {
  if(a.length !== b.length) return false;
  if(a === b) return a.length > new Set(a).size;
  let temp = [];
  for (let i = 0; i < a.length; i++) {
    if(a[i] !== b[i]){
      temp.push(a[i], b[i]);
    }
  }
  return (temp.length === 4) && (temp[0] === temp[3]) && (temp[1] === temp[2])
}
// @lc code=end

